package com.jspx.txweb.dao.impl;

import com.jspx.boot.sign.CongealType;
import com.jspx.boot.sign.UserType;
import com.jspx.boot.sign.YesNoType;
import com.jspx.sober.SqlMapClient;
import com.jspx.sober.criteria.Order;
import com.jspx.sober.jdbc.JdbcOperations;
import com.jspx.txweb.IMember;
import com.jspx.txweb.dao.MemberDAO;
import com.jspx.txweb.dao.TreeItemDAO;
import com.jspx.txweb.table.*;
import com.jspx.utils.*;
import org.slf4j.Logger;
import com.jspx.boot.environment.Environment;
import org.slf4j.LoggerFactory;
import com.jspx.sober.Criteria;
import com.jspx.sober.TableModels;
import com.jspx.sober.criteria.expression.Expression;
import com.jspx.sober.criteria.projection.Projections;
import com.jspx.sober.ssql.SSqlExpression;
import com.jspx.txweb.dao.SensitiveWordDAO;

import java.sql.SQLException;
import java.util.*;


public class SensitiveWordDAOImpl extends DFAFilterImpl implements SensitiveWordDAO {

    protected static final Logger log = LoggerFactory.getLogger(SensitiveWordDAOImpl.class);
    /**
     * 构造函数，初始化敏感词库
     */
    public SensitiveWordDAOImpl(){

    }


    public boolean deleteAll()
    {
        return createCriteria(SensitiveWord.class).delete(false) > 0;
    }


    /**
     * @param find  查询
     * @param term  条件
     * @param sort  排序方式
     * @param page  页数
     * @param count 数量
     * @return 得到列表
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public List<SensitiveWord> getList(String[] fields, String[] find, String term, String sort, int page, int count) throws Exception
    {
        Criteria criteria = createCriteria(SensitiveWord.class);
        if (!ArrayUtil.isEmpty(find) && !ArrayUtil.isEmpty(fields)) {
            criteria = criteria.add(Expression.find(fields, find));
        }
        criteria = SSqlExpression.getTermExpression(criteria, term);
        criteria = SSqlExpression.getSortOrder(criteria, sort);
        return (List<SensitiveWord>) criteria.setCurrentPage(page).setTotalCount(count).list(false);
    }

    public Set<String> getOriginal(Set<String> words) throws Exception
    {
        return words;
    }

    /**
     * @param find 查询
     * @param term 条件
     * @return 返回数据条数
     * @throws Exception 异常
     */
    public int getCount(String[] fields, String[] find, String term) throws Exception
    {
        Criteria criteria = createCriteria(SensitiveWord.class);
        if (!ArrayUtil.isEmpty(find) && !ArrayUtil.isEmpty(fields)) {
            criteria = criteria.add(Expression.find(fields, find));
        }
        criteria = SSqlExpression.getTermExpression(criteria, term);
        return criteria.setProjection(Projections.rowCount()).intUniqueResult();
    }

    public int getCount()
    {
        Criteria criteria = createCriteria(SensitiveWord.class);
        return criteria.setProjection(Projections.rowCount()).intUniqueResult();
    }

    public boolean hasWord(String word) throws Exception
    {
        if (StringUtil.isNULL(word)) return false;
        Criteria criteria = createCriteria(SensitiveWord.class).add(Expression.eq("word", word));
        return criteria.setProjection(Projections.rowCount()).intUniqueResult() > 0;
    }

    //------------------------------------------------------------------------------------------------------------------

    /**
     * @param txt 导入文本
     * @return 导入行数
     * @throws Exception
     */
    public int importWord(String txt) throws Exception
    {
        int saveRow = 0;
        List<SensitiveWord> saveSensitiveWordList = new ArrayList<SensitiveWord>();
        List<String> checkList = new ArrayList<String>();
        String[] lines = StringUtil.split(StringUtil.convertCR(txt), StringUtil.CR);
        for (String line : lines) {
            if (StringUtil.isNULL(line) || checkList.contains(line)) continue;
            SensitiveWord sensitiveWord = new SensitiveWord();
            sensitiveWord.setWord(line);

            sensitiveWord.setIp(Environment.localeIP);
            sensitiveWord.setPutUid(Environment.SYSTEM_ID);
            sensitiveWord.setPutName(Environment.SYSTEM_NAME);
            saveSensitiveWordList.add(sensitiveWord);

            checkList.add(line);

            if (saveSensitiveWordList.size() > 500) {
                super.batchSave(saveSensitiveWordList);
                saveSensitiveWordList.clear();
            }

        }
        if (!saveSensitiveWordList.isEmpty()) {
            super.batchSave(saveSensitiveWordList);
            saveSensitiveWordList.clear();
        }

        checkList.clear();
        return saveRow;
    }

    public int checkImportWord(String txt) throws Exception
    {
        int saveRow = 0;
        String[] lines = StringUtil.split(StringUtil.convertCR(txt), StringUtil.CR);
        for (String line : lines) {
            if (StringUtil.isNULL(line)) continue;
            if (hasWord(line)) continue;
            SensitiveWord blockedWord = new SensitiveWord();
            blockedWord.setWord(line);
            blockedWord.setIp(Environment.localeIP);
            blockedWord.setPutUid(Environment.SYSTEM_ID);
            blockedWord.setPutName(Environment.SYSTEM_NAME);
            saveRow = saveRow + super.save(blockedWord);
        }
        return saveRow;
    }

    public int updateTimes(Set<String> keys)
    {
        TableModels soberTable = getSoberTable(SensitiveWord.class);
        StringBuilder sb = new StringBuilder();
        for (String key:keys)
        {
            sb.append(StringUtil.quote(key,false)).append(",");
        }
        if (sb.toString().endsWith(","))
        {
            sb.setLength(sb.length()-1);
        }
        String sql = "UPDATE "+ soberTable.getTableName() +" SET times=times+1 WHERE word IN(" + sb.toString() + ")";
        return super.update(sql);
    }
    //------------------------------------------------------------------------------------------------------------------
    public void init() throws Exception
    {
        if (!wordMap.isEmpty()) return;
        log.info("开始载入敏感词库");
        int maxRows = super.getSoberFactory().getMaxRows();
        try {
            int maxWord = getCount();
            if (maxWord>100000) maxWord = 100000;
            super.getSoberFactory().setMaxRows(maxWord);
            List<SensitiveWord> list = getList(null, null, null, null, 1, maxWord);
            Set<String> keyWordSet = new HashSet<String>();
            for (SensitiveWord sensitiveWord : list)
            {
                keyWordSet.add(sensitiveWord.getWord());
            }
            addWordToHashMap(keyWordSet);
            list.clear();
            keyWordSet.clear();
            log.info("开始载入敏感词结束,共载入" + wordMap.size() + "个");
        } finally {
            super.getSoberFactory().setMaxRows(maxRows);
        }
    }

}
